package gui;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.trolltech.qt.gui.*;

import gestionBDD.extractionBDD;
import gestionBDD.requetesAlbum;
import gestionBDD.requetesContient;
import gestionBDD.requetesImage;
import interne.*;

public class fenetrePhoto extends QWidget {
	
	Client client ;
	Album album ;
	Contient contient ;
	
	QListWidget photosListWidget;
	QTextBrowser detailsTextDocument ;
	
	public fenetrePhoto(Client c, Album a) throws SQLException {
		
		setClient(c) ;
		setAlbum(a) ;
		
		try {
			photosListWidget = new QListWidget();
			photosListWidget.currentItemChanged.connect(this, "afficherDetails()");

			detailsTextDocument = new QTextBrowser();

			/** Login's GroupBox' **/
	        QGroupBox photosGroup = new QGroupBox(tr("Photos contenues dans l'album personnel"));
	        QHBoxLayout photoslayout = new QHBoxLayout();
	        photoslayout.addWidget(photosListWidget);
	        photoslayout.addWidget(detailsTextDocument);
	        photosGroup.setLayout(photoslayout);

	        /* Bouton Editer */
	        QPushButton boutonEditer = new QPushButton("Editer...");
	        boutonEditer.clicked.connect(this, "editerPhoto()"); 	
	        	        
	        /* Bouton Uploader Photo */
	        QPushButton boutonUploaderPhoto = new QPushButton("Uploader Photo");
	        boutonUploaderPhoto.clicked.connect(this, "popUploadPhoto()");  
	        /* Bouton Ajouter Photo */
	        QPushButton boutonAjouterPhoto = new QPushButton("Ajouter Photo");
	        boutonAjouterPhoto.clicked.connect(this, "popAjoutPhoto()");  
	        /* Bouton Retirer Photo */
	        QPushButton boutonRetirerPhoto = new QPushButton("Retirer");
	        boutonRetirerPhoto.clicked.connect(this, "retirerPhoto()");
	        
	        /* Bouton Rafraichir */
	        QPushButton boutonRefresh = new QPushButton("Refresh");
	        boutonRefresh.clicked.connect(this, "reactualiser()");  
	        
	        /* Bouton Retour */
	        QPushButton boutonRetour = new QPushButton("Retour");
	        boutonRetour.clicked.connect(this, "retour()");  
			
	        /** Login's GroupBox' **/
	        QGroupBox boutonsGroup = new QGroupBox(tr("Actions"));
	        QGridLayout boutonLayout = new QGridLayout();

	        boutonLayout.addWidget(boutonEditer, 0, 0);
	        boutonLayout.addWidget(boutonUploaderPhoto, 0 , 1);
	        boutonLayout.addWidget(boutonRetirerPhoto, 0 , 2);
	        boutonLayout.addWidget(boutonRefresh, 1, 1);
	        boutonLayout.addWidget(boutonAjouterPhoto, 1, 0);
	        boutonLayout.addWidget(boutonRetour, 1, 2);
	        boutonsGroup.setLayout(boutonLayout);
	        
	        QVBoxLayout layout = new QVBoxLayout();
	        layout.addWidget(photosGroup);
	        layout.addWidget(boutonsGroup);
	        setLayout(layout);
	        
			List<Contient> listePhoto = new ArrayList<Contient>(); 
			listePhoto = extractionBDD.extrairePhotos(album);
			Iterator<Contient> ite = listePhoto.iterator();
			while (ite.hasNext()) {
			    Contient photo = ite.next();
			    photosListWidget.addItem(photo.getOrdre()+ " : " + photo.getTitreImage() + " / " + photo.getIdContient()) ;
			}
		}
		catch(SQLException e) {
			System.out.println(e) ;
		}
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}
	
	public Contient getContient() {
		return contient;
	}

	public void setContient(Contient contient) {
		this.contient = contient;
	}

	public void popUploadPhoto() {
		fenetreUploadPhoto fenetre = new fenetreUploadPhoto(album) ;
		fenetre.show();
	}
	
	public void afficherDetails() throws SQLException {
		String idContenu = photosListWidget.currentItem().text() ;
		String temp[] = idContenu.split(" / ");
		idContenu = temp[1] ;
		Contient contient = new Contient(Integer.parseInt(idContenu)) ;
		setContient(requetesContient.trouverAvecId(contient)) ;
		Image image = new Image(contient.getIdImage()); 
		image = requetesImage.trouverAvecId(image);
		
		detailsTextDocument.clear() ;
		detailsTextDocument.setText("Titre Album : " + requetesAlbum.retourneTitreAlbum(contient.getIdAlbum())  + "\n" +
				"Ordre : " + contient.getOrdre() + "\n" +
				"Titre Photo : " + contient.getTitreImage() + "\n" +
				"Commentaire : " + contient.getCommentaire() + "\n\n" +
				"Chemin : " + image.getChemin() + "\n" +
				"Appareil Photo : " + image.getAppareilPhoto() + "\n" +
				"Resolution : " + image.getResolution() + "\n" +
				"Objectif : " + image.getObjectif() + "\n" +
				"Distance focale : " + image.getDistFocale() + "\n" +
				"Sensibilite ISO : " + image.getSensibiliteISO() + "\n" +
				"Ouverture : " + image.getOuverture() + "\n" +
				"Vitesse Obturation : " + image.getVitesseObturation()) ; 
	}
	
	public void popAjoutPhoto() throws SQLException {
		fenetreAjoutPhoto fenetre = new fenetreAjoutPhoto(client, album) ;
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
	
	public void editerPhoto() throws SQLException {
		String titreImage = photosListWidget.currentItem().text() ;
		String temp[] = titreImage.split(" / ");
		Contient c = new Contient(Integer.parseInt(temp[1])) ;
		c = requetesContient.trouverAvecId(c) ;
		fenetrePhotoEdition fenetre = new fenetrePhotoEdition(c);
		fenetre.show();
	}
	
	public void retirerPhoto() throws SQLException {
		String titreImage = photosListWidget.currentItem().text() ;
		String temp[] = titreImage.split(" / ");
		Contient contient = new Contient(Integer.parseInt(temp[1])) ;
		contient = requetesContient.trouverAvecId(contient);
		requetesContient.supprimerContient(contient) ;
		reactualiser() ;
	}
	
	public void reactualiser() throws SQLException {
        fenetrePhoto fenetre = new fenetrePhoto(client, album);
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
	
	public void retour() throws SQLException {
        fenetreAlbumPriv fenetre = new fenetreAlbumPriv(client);
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
}
